home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / checkbox / registry.py < prev    next >
Text File  |  2009-11-05  |  5KB  |  186 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import re
  20.  
  21. from checkbox.lib.cache import cache
  22.  
  23. from checkbox.component import ComponentManager
  24. from checkbox.frontend import FrontendException
  25. from checkbox.properties import String
  26.  
  27.  
  28. class Registry(object):
  29.     """
  30.     Registry base class which should be inherited by each registry
  31.     implementation. This class basically provides methods to represent
  32.     the items in the registry as attributes. If some items cannot
  33.     be represented as attributes, if there are spaces in the name
  34.     for example, this class also provides methods to reprent them as
  35.     dictionary elements.
  36.     """
  37.  
  38.     _id = 0
  39.  
  40.     user = String(required=False)
  41.  
  42.     def __init__(self):
  43.         super(Registry, self).__init__()
  44.         self.id = Registry._id
  45.         Registry._id += 1
  46.  
  47.     def __str__(self):
  48.         raise NotImplementedError, "this function must be overridden by subclasses"
  49.  
  50.     def __getattr__(self, name):
  51.         return self.get(name)
  52.  
  53.     def split(self, *args, **kwargs):
  54.         return str(self).split(*args, **kwargs)
  55.  
  56.     def items(self):
  57.         raise NotImplementedError, "this function must be overridden by subclasses"
  58.  
  59.     def get(self, key, default=None):
  60.         try:
  61.             return self.__getitem__(key)
  62.         except KeyError:
  63.             if default == None:
  64.                 from checkbox.registries.none import NoneRegistry
  65.                 return NoneRegistry()
  66.             else:
  67.                 return default
  68.  
  69.     def has_key(self, key):
  70.         return key in self.keys()
  71.  
  72.     def iteritems(self):
  73.         for k, v in self.items():
  74.             yield k, v
  75.  
  76.     def iterkeys(self):
  77.         from checkbox.registries.link import LinkRegistry
  78.  
  79.         for k, v in self.items():
  80.             # Prevent returning links in a dict() context
  81.             if not isinstance(v, LinkRegistry):
  82.                 yield k
  83.  
  84.     def keys(self):
  85.         return list(self.iterkeys())
  86.  
  87.     def itervalues(self):
  88.         from checkbox.registries.link import LinkRegistry
  89.  
  90.         for k, v in self.items():
  91.             # Prevent returning links in a values() context
  92.             if not isinstance(v, LinkRegistry):
  93.                 yield v
  94.  
  95.     def values(self):
  96.         return list(self.itervalues())
  97.  
  98.     def clear(self):
  99.         raise Exception, "Cannot call clear on registry."
  100.  
  101.     def setdefault(self, key, default=None):
  102.         raise Exception, "Cannot call setdefault on registry."
  103.  
  104.     def __cmp__(self, foreign):
  105.         local = set(self.items())
  106.         foreign = set(foreign.items())
  107.         if local == foreign:
  108.             return 0
  109.         elif local < foreign:
  110.             return -1
  111.         return 1
  112.  
  113.     def __contains__(self, key):
  114.         return key in self.keys()
  115.  
  116.     def __len__(self):
  117.         return len(self.keys())
  118.  
  119.     def __getitem__(self, key):
  120.         for k, v in self.items():
  121.             if k == key:
  122.                 return v
  123.  
  124.         raise KeyError
  125.  
  126.     def __setitem__(self, key, value):
  127.         raise Exception, "Cannot set setitem on registry."
  128.  
  129.     def __delitem__(self, key):
  130.         raise Exception, "Cannot call delitem on registry."
  131.  
  132.     def update(self, foreign):
  133.         raise Exception, "Cannot call update on registry."
  134.  
  135.  
  136. class RegistryManager(ComponentManager, Registry):
  137.     """
  138.     Registry manager which is essentially the root of the registry
  139.     tree. The first level in this tree consists of the module names
  140.     which have been loaded from the registries configuration parameter.
  141.     """
  142.  
  143.     @cache
  144.     def items(self):
  145.         items = []
  146.         registries = self._config.get_defaults().registries
  147.         section_names = re.split(r"\s+", registries)
  148.         for section_name in section_names:
  149.             section = self.load_section(section_name)
  150.             for name in section.get_names():
  151.                 module = section.load_module(name)
  152.                 items.append((name, module))
  153.  
  154.         return items
  155.  
  156.  
  157. def registry_eval(registry, source):
  158.     try:
  159.         return eval(source, {}, registry)
  160.     except Exception:
  161.         return False
  162.  
  163. def registry_eval_recursive(registry, source, mask=[False]):
  164.     values = []
  165.  
  166.     value = registry_eval(registry, source)
  167.     if type(value) in (bool, int) and value:
  168.         values.append(registry)
  169.         mask[0] = True
  170.     elif type(value) is tuple and True in value:
  171.         for i in range(len(value)):
  172.             if value[i] is True or i >= len(mask):
  173.                 mask[i:i+1] = [value[i]]
  174.  
  175.         values.append(registry)
  176.  
  177.     try:
  178.         for key, value in registry.items():
  179.             if isinstance(value, Registry):
  180.                 values.extend(registry_eval_recursive(value, source, mask))
  181.     except FrontendException:
  182.         # Failing to call the backend should result in a failed eval
  183.         mask[0] = False
  184.  
  185.     return values
  186.